package com.hup.joystick.model;

import com.hup.joystick.constant.FunctionConstant.QuickMenuFunctionType;
import com.hup.joystick.service.QuickMenuFunctions;
import com.hup.joystick.view.quickMenu.setting.QMSFunctionItem;
import com.hup.utils.commons.io.BaseConfig;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;

/**
 * 快捷菜单配置
 *
 * @author hugan
 * @date 2020/11/10
 */
@Data
public class QuickMenuConfig implements BaseConfig<QuickMenuConfig> {

    public ArrayList<QuickMenuFunction> lstFunction = new ArrayList<>();

    public ArrayList<String> lstApp = new ArrayList<>();

    /**
     * 将界面配置转成数据对象;
     * .跳过[键盘按键]为空的item
     */
    public void viewToFunction(QMSFunctionItem view) {
        QuickMenuFunctionType type = view.getType();
        QuickMenuFunction function = new QuickMenuFunction();
        function.type = type;
        if (type == QuickMenuFunctionType.NONE) {
            return;
        } else if (type == QuickMenuFunctionType.CMD) {
            QuickMenuFunction selectedItem = view.cbInnerFunction.getSelectedItem();
            if (selectedItem == null) return;//不会发生的
            function.cmdDesc = selectedItem.cmdDesc;
        } else if (type == QuickMenuFunctionType.KEYBOARD) {
            //不能直接赋值为view的list
            function.lstKeyCode = new ArrayList<>(view.getKeyCode());
        }
        lstFunction.add(function);
    }

    /**
     * 加载文件配置,如果[无文件配置,配置异常],则加载默认的[内置功能]
     */
    @Override
    public QuickMenuConfig loadFromFile() {
        QuickMenuConfig file = readConfigFile();
        if (file != null) {
            lstFunction.clear();
            lstApp.clear();
            BeanUtils.copyProperties(file, this);
        } else {
            toDefaultConfig();
        }
        return this;
    }

    /**
     * 还原至默认配置;
     * .仅含[内置功能]
     */
    public void toDefaultConfig() {
        lstFunction.clear();
        lstFunction.addAll(QuickMenuFunctions.getBuildInFunctions());
        lstApp.clear();
    }

}
